/*******************************************************************************
*
*               COPYRIGHT (c) 2015-2016 Broadlink Corporation
*                         All Rights Reserved
*
* The source code contained or described herein and all documents
* related to the source code ("Material") are owned by Broadlink
* Corporation or its licensors.  Title to the Material remains
* with Broadlink Corporation or its suppliers and licensors.
*
* The Material is protected by worldwide copyright and trade secret
* laws and treaty provisions. No part of the Material may be used,
* copied, reproduced, modified, published, uploaded, posted, transmitted,
* distributed, or disclosed in any way except in accordance with the
* applicable license agreement.
*
* No license under any patent, copyright, trade secret or other
* intellectual property right is granted to or conferred upon you by
* disclosure or delivery of the Materials, either expressly, by
* implication, inducement, estoppel, except in accordance with the
* applicable license agreement.
*
* Unless otherwise agreed by Broadlink in writing, you may not remove or
* alter this notice or any other notice embedded in Materials by Broadlink
* or Broadlink's suppliers or licensors in any way.
*
*******************************************************************************/

#include "dna_json.h"
#include "dna_errno.h"
#include <json.h>

#define DNA_JSON_RESULT(result) (((result) != 0) ? (-(result)) - 28671 : DNA_SUCCESS)

int dna_json_object_init(dna_json_object_t * jobj, const char * buff)
{
    struct json_object * obj = (struct json_object *)jobj;
    
    return -DNA_JSON_RESULT(json_object_init(obj, buff));
}

int dna_json_object_split(
        dna_json_object_t * jobj,
        char * buffer, int maxsize, dna_json_keyval_list_t * list)
{
    struct json_object * obj = (struct json_object *)jobj;

    return -DNA_JSON_RESULT(json_object_split(obj, buffer, maxsize, \
                (json_keyval_list_t *)list));
}

int dna_json_get_composite_object(dna_json_object_t * jobj, const char * name)
{
    struct json_object * obj = (struct json_object *)jobj;

    return -DNA_JSON_RESULT(json_get_composite_object(obj, name));
}

int dna_json_release_composite_object(dna_json_object_t * jobj)
{
    struct json_object * obj = (struct json_object *)jobj;

    return -DNA_JSON_RESULT(json_release_composite_object(obj));
}

int dna_json_get_array_object(dna_json_object_t * jobj, const char * name)
{
    struct json_object * obj = (struct json_object *)jobj;

    return -DNA_JSON_RESULT(json_get_array_object(obj, name));
}

int dna_json_get_array_int(dna_json_object_t * jobj, int * val)
{
    struct json_object * obj = (struct json_object *)jobj;

    return -DNA_JSON_RESULT(json_get_array_int(obj, val));
}

int dna_json_get_array_float(dna_json_object_t * jobj, float * val)
{
    struct json_object * obj = (struct json_object *)jobj;

    return -DNA_JSON_RESULT(json_get_array_float(obj, val));
}

int dna_json_get_array_string(
        dna_json_object_t * jobj, const char * name,
        char * buffer, int maxsize, dna_json_val_list_t * list)
{
    struct json_object * obj = (struct json_object *)jobj;

    return -DNA_JSON_RESULT(json_get_array_string(obj, name, buffer, maxsize, \
                (json_val_list_t *)list));
}

char * dna_json_get_array_subobj(
            dna_json_object_t * jobj, const char * name,
            char * buffer, int maxsize, dna_json_val_list_t * list)
{
    struct json_object * obj = (struct json_object *)jobj;

    return json_get_array_subobj(obj, name, buffer, maxsize, (json_val_list_t *)list);
}

int dna_json_release_array_object(dna_json_object_t * jobj)
{
    struct json_object * obj = (struct json_object *)jobj;

    return -DNA_JSON_RESULT(json_release_array_object(obj));
}

int dna_json_get_val_str(dna_json_object_t * jobj, const char * name, char * val, int maxlen)
{
    struct json_object * obj = (struct json_object *)jobj;

    return -DNA_JSON_RESULT(json_get_val_str(obj, name, val, maxlen));
}

int dna_json_get_val_int(dna_json_object_t * jobj, const char * name, int * val_int)
{
    struct json_object * obj = (struct json_object *)jobj;

    return -DNA_JSON_RESULT(json_get_val_int(obj, name, val_int));
}

int dna_json_get_val_long_long(dna_json_object_t * jobj, const char * name, long long * val_int)
{
    struct json_object * obj = (struct json_object *)jobj;

    return -DNA_JSON_RESULT(json_get_val_long_long(obj, name, val_int));
}
int dna_json_get_val_float(dna_json_object_t * jobj, const char * name, float * val)
{
    struct json_object * obj = (struct json_object *)jobj;

    return -DNA_JSON_RESULT(json_get_val_float(obj, name, val));
}

int dna_json_str_init(dna_json_object_t * jobj, char * buff, int len)
{
    struct json_str * jptr = (struct json_str *)jobj;

    json_str_init(jptr, buff, len, 0);

    return DNA_SUCCESS;
}

int dna_json_str_len(dna_json_object_t * jobj)
{
    struct json_str * jptr = (struct json_str *)jobj;

    /* Check */
    if(jptr->buff == NULL || jptr->len == 0)
        return 0;

    return jptr->free_ptr;
}

int dna_json_start_object(dna_json_object_t * jobj)
{
    struct json_str * jptr = (struct json_str *)jobj;

    return -DNA_JSON_RESULT(json_start_object(jptr));
}

int dna_json_push_object(dna_json_object_t * jobj, const char * name)
{
    struct json_str * jptr = (struct json_str *)jobj;

    return -DNA_JSON_RESULT(json_push_object(jptr, name));
}

int dna_json_pop_object(dna_json_object_t * jobj)
{
    struct json_str * jptr = (struct json_str *)jobj;

    return -DNA_JSON_RESULT(json_pop_object(jptr));
}

int dna_json_push_array_object(dna_json_object_t * jobj, const char * name)
{
    struct json_str * jptr = (struct json_str *)jobj;

    return -DNA_JSON_RESULT(json_push_array_object(jptr, name));
}

int dna_json_pop_array_object(dna_json_object_t * jobj)
{
    struct json_str * jptr = (struct json_str *)jobj;

    return -DNA_JSON_RESULT(json_pop_array_object(jptr));
}

int dna_json_close_object(dna_json_object_t * jobj)
{
    struct json_str * jptr = (struct json_str *)jobj;

    return -DNA_JSON_RESULT(json_close_object(jptr));
}

int dna_json_close_array(dna_json_object_t * jobj)
{
	struct json_str * jptr = (struct json_str *)jobj;

    return -DNA_JSON_RESULT(json_close_array(jptr));
}	

int dna_json_set_val_str(dna_json_object_t * jobj, const char * name,
			  const char * val)
{    
    struct json_str * jptr = (struct json_str *)jobj;

    return -DNA_JSON_RESULT(json_set_val_str(jptr, name, val));
}

int dna_json_set_val_int(dna_json_object_t * jobj, const char *name,
			  int val)
{
    struct json_str * jptr = (struct json_str *)jobj;

    return -DNA_JSON_RESULT(json_set_val_int(jptr, name, val));
}


int dna_json_set_val_uint(dna_json_object_t * jobj, const char *name,
			  unsigned int val)
{
    struct json_str * jptr = (struct json_str *)jobj;

    return -DNA_JSON_RESULT(json_set_val_uint(jptr, name, val));
}

int dna_json_set_val_float(dna_json_object_t * jobj, const char *name,
			  float val)
{
    struct json_str * jptr = (struct json_str *)jobj;

    return -DNA_JSON_RESULT(json_set_val_float(jptr, name, val));
}

int dna_json_set_array_str(dna_json_object_t * jobj, const char * val)
{    
    struct json_str * jptr = (struct json_str *)jobj;

    return -DNA_JSON_RESULT(json_set_array_str(jptr, val));
}

int dna_json_set_array_int(dna_json_object_t * jobj, int val)
{
    struct json_str * jptr = (struct json_str *)jobj;

    return -DNA_JSON_RESULT(json_set_array_int(jptr, val));
}

int dna_json_set_array_float(dna_json_object_t * jobj, float val)
{
    struct json_str * jptr = (struct json_str *)jobj;

    return -DNA_JSON_RESULT(json_set_array_float(jptr, val));
}

